package com.training.schedule_management_system_admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.training.schedule_management_system_admin.entity.User;
import com.training.schedule_management_system_admin.service.UserService;
import com.training.schedule_management_system_admin.utils.EmailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    UserService userService;

    /*
    开放/关闭注册
     */
    @RequestMapping("/setRegisterStatus")
    public String adminRegister(@RequestParam(value = "flag",defaultValue = "true") String flag,Model model){
        JSONObject jsonObject=new JSONObject();
        stringRedisTemplate.opsForValue().set("是否开放注册",flag);
        String result = stringRedisTemplate.opsForValue().get("是否开放注册");
        jsonObject.put("当前是否开放注册",result);
//        return Msg.success().add("success",jsonObject);
//        model.addAttribute("state",jsonObject);
        return "redirect:/admin/list";
    }

    /*
    用户列表
     */
    @RequestMapping("/list")
    public String userList(@RequestParam(value = "pageNum",defaultValue ="1") int pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                           @RequestParam(value = "navigatePages",defaultValue = "5") int navigatePages,
                           Model model){
        JSONObject jsonObject=new JSONObject();
        String result = stringRedisTemplate.opsForValue().get("是否开放注册");
        jsonObject.put("当前是否开放注册",result);

        PageHelper.startPage(pageNum, pageSize);//当前页码，每页的记录数
        List<User> users=userService.getAll();
        PageInfo page=new PageInfo(users,navigatePages);//连续显示的页数
        jsonObject.put("page",page);
//        return Msg.success().add("success",jsonObject);
        model.addAttribute("msg",jsonObject);
        return "admin/list";
    }

    /*
    审核用户
     */
    @RequestMapping("/audit/{userId}")
    public String auditUser(@PathVariable("userId")Integer userId,Model model){
        JSONObject jsonObject=new JSONObject();
        User user = userService.getUserWithRole(userId);
        if(user==null){
            jsonObject.put("user","用户不存在");
            model.addAttribute("failed",jsonObject);
            return "admin/result";
        }else{
            if(user.getAuditState()==0){
                userService.auditUser(user);
                EmailUtils.sendEmailAudit(user);
                jsonObject.put("message","用户"+user.getUsername()+"已审核完成，已发送邮件通知其可以登录！");
            }else{
                jsonObject.put("message","用户"+user.getUsername()+"已审核，无需重复操作！");
            }
        }
        jsonObject.put("user",user);
        model.addAttribute("success",jsonObject);
        return "admin/result";
    }

}
